<?php

 
 Zend_Loader::loadClass("BaseDBRows");
 Zend_Loader::loadClass("Organism");
 class Organisms extends BaseDBRows
 {
 	protected function setUp()
 	{
 		$this->_tableName = Organism::_tablename;
 	}	
	
	public function updateAttributes() {
		Zend_Loader::loadClass("Size");
		Zend_Loader::loadClass("Organism");
		Zend_Loader::loadClass("OrganismAttribute");
		Zend_Loader::loadClass("QueryCreator");
		
		$db = DBCon::instance();		
		$qyr_sizes = new QueryCreator();
		$qry_organisms = new QueryCreator();
		
		$qry_organisms->addSelect("* ");
		$qry_organisms->addFrom(Organism::_tablename . " as qryorganism ");
		
		print $qry_organisms->createSQL();print "<br>";
		
		$organisms = $db->executeQuery($qry_organisms->createSQL());
		
		$qyr_sizes->addSelect("* ");
		$qyr_sizes->addFrom(Size::_tablename . " as qrysize ");			

		$qyr_attributes = new QueryCreator();
		
		$sizes = $db->executeQuery($qyr_sizes->createSQL());
		print "<br>";		
		foreach($organisms as $organism) {
			foreach($sizes as $size) {	
				$qyr_attributes->clear();			
				$qyr_attributes->addSelect(" count(*) as cnt ");
				$qyr_attributes->addFrom(OrganismAttribute::_tablename." as e ");
				$qyr_attributes->addWhere("e.organism_id = ".$organism['id']." AND e.size_code = '".$size['size_code']."' ");
				$attributes = $db->executeQuery($qyr_attributes->createSQL());	
				
				if ($attributes[0]['cnt'] > 0) {
					print "Found : ";
					//print $organism['id']." ";
					//print $organism['name']." ";
					//print $size['size_code']." ";					
					print "<br>";
				} else {	
					print "Not Found : ";
					print $organism['id']." ";
					print $organism['name']." ";
					print $size['size_code']." ";						
					print "<br>";
					
					$org=new OrganismAttribute();
					$org->organism_id=$organism['id'];
					$org->size_code=$size['size_code'];
					$org->date_created=date("Y-m-d H:i:s");
					$org->date_updated=date("Y-m-d H:i:s");
					$org->created_by="ekimo";
					$org->updated_by="ekimo";
					$org->save();
					$org->organism_attribute_id=$org->id;
					$org->save();				
				}	
								
			}
		}		
	}
	public function getOrgInventoryByOrgId($orgId) 
	{
		Zend_Loader::loadClass("Organization");
		Zend_Loader::loadClass("Inventory");
		Zend_Loader::loadClass("Tank");
		Zend_Loader::loadClass("OrganizationOrganism");
		Zend_Loader::loadClass("OrganismAttribute");
		Zend_Loader::loadClass("Size");
		Zend_Loader::loadClass("Organism");
		Zend_Loader::loadClass("OrganismGroup");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("t.name tank_name, o.moid, o.name common_name, s.size, sum(i.quantity_on_hand) total_qty_on_hand ");
		$query->addSelect("sum(i.quantity_on_shipment) total_qty_on_shipment, i.currency_mark, sum(i.stock_amount) total_stock_amount ");
		$query->addSelect("o.organism_group_id, og.organism_group");
		$query->addSelect("ifnull((select 1 from inventory_daa where inventory_id = i.inventory_id),0) has_daa");
		$query->addFrom($this->_tableName . " as i ");
		$query->addJoin(" left join " . Tank::_tablename . " t on i.tank_id = t.tank_id and i.org_id = t.org_id ");
		$query->addJoin(OrganizationOrganism::_tablename . " oo ");
		$query->addJoin(OrganismAttribute::_tablename . " oa ");
		$query->addJoin(" left join " . Size::_tablename . " on oa.size_code = s.size_code ");
		$query->addJoin(Organism::_tablename . " o ");
		$query->addJoin(" left join " . OrganismGroup::_tablename . " og on o.organism_group_id = og.organism_group_id ");
		$query->addWhere(" i.org_id = oo.org_id " );
		$query->addWhere(" i.org_organism_id = oo.org_organism_id ");
		$query->addWhere(" i.organism_attribute_id = oa.organism_attribute_id ");
		$query->addWhere(" oo.organism_id = o.organism_id ");
		$query->addWhere(" i.org_id = " . $orgId);
		$query->addGroupBy(" t.name, o.moid, o.name, s.size, i.currency_mark, og.organism_group, o.organism_group_id ");
		
		$db = DBCon::instance();
		$result = $db->executeQuery($query->createSQL());
		return $result;
	}
	
	
	public function getMOIDByOrgId($orgId)
	{
		Zend_Loader::loadClass("OrganizationOrganism");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();	
		$query->addSelect("o.moid");
		$query->addFrom($this->_tableName  . " as o ");
		$query->addJoin(" left join " . OrganizationOrganism::_tablename . " as oo on o.organism_id = oo.organism_id ");
		$query->addWhere(" oo.org_id = " . $orgId);
		$query->addGroupBy(" moid ");
		
		$db = DBCon::instance();
		$result = $db->executeQuery($query->createSQL());
		return $result;
	}
	
	public function getInvMOIDByOrgId($orgId)
	{
		Zend_Loader::loadClass("OrganizationOrganism");
		Zend_Loader::loadClass("Inventory");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("o.moid");
		$query->addFrom($this->_tableName . " o ");
		$query->addJoin("," . OrganizationOrganism::_tablename . " oo ");
		$query->addJoin("," . Inventory::_tablename . " i ");
		$query->addWhere("o.organism_id = oo.organism_id");
		$query->addWhere("oo.org_organism_id = i.org_organism_id");
		$query->addWhere("oo.org_id = i.org_id");
		$query->addWhere(" i.org_id " . $orgId);
		$query->addGroupBy("o.moid");
		
		$db = DBCon::instance();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	
	
	
 }
?>